package com.modifymetrodemo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.modifymetrodemo.async.AsyncLoadBusinessParentActivity;
import com.modifymetrodemo.async.AsyncLoadBusinessSingleActivity;
import com.modifymetrodemo.beans.ReactNativeBusinessInfo;
import com.modifymetrodemo.utils.SharedPreferenceUtils;
import com.modifymetrodemo.utils.TimeRecordUtils;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {

    private Button mBtnAysncLoadBusinessWithOneActivity,
            mBtnAysncLoadBusinessWithTwoActivities;
    private Spinner mSpinerChooseRNBusiness;

    private Context mContext;

    private ReactNativeBusinessInfo mChosenBusinessInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_welcome);
        mBtnAysncLoadBusinessWithOneActivity = findViewById(R.id.btn_go_to_async_load_with_one_activity);
        mBtnAysncLoadBusinessWithOneActivity.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, AsyncLoadBusinessSingleActivity.class);
            intent.putExtra("displayName",mChosenBusinessInfo.displayName);
            intent.putExtra("bundleFilePath",mChosenBusinessInfo.bundleFilePath);
            intent.putExtra("mainComponentName",mChosenBusinessInfo.mainComponentName);
            startActivity(intent);
            TimeRecordUtils.setStartTime();
        });

        mBtnAysncLoadBusinessWithTwoActivities = findViewById(R.id.btn_go_to_async_load_with_two_activities);
        mBtnAysncLoadBusinessWithTwoActivities.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, AsyncLoadBusinessParentActivity.class);
            intent.putExtra("displayName",mChosenBusinessInfo.displayName);
            intent.putExtra("bundleFilePath",mChosenBusinessInfo.bundleFilePath);
            intent.putExtra("mainComponentName",mChosenBusinessInfo.mainComponentName);
            startActivity(intent);
        });

        mSpinerChooseRNBusiness = findViewById(R.id.spinner_choose_rn_business);

        final List<ReactNativeBusinessInfo> testItems = new ArrayList<>();
        testItems.add(new ReactNativeBusinessInfo("简单RN业务",
                "assets://diff.android.simple.bundle",
                "simple"));
        testItems.add(new ReactNativeBusinessInfo("较复杂RN业务",
                "assets://diff.android.complex.bundle",
                "complex"));

        mSpinerChooseRNBusiness.setAdapter(new MyReactNativeBusinessAdapter(testItems));

        mSpinerChooseRNBusiness.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferenceUtils.saveChosenBusinessAssetsPath(mContext, testItems.get(position).bundleFilePath);
                mChosenBusinessInfo = testItems.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // mActivateCodeEt.setText("");
            }
        });

        if (!SharedPreferenceUtils.hasChosenBusinessAssetsPath(mContext)) {
            mSpinerChooseRNBusiness.setSelection(0);
        } else {
            for (int i = 0; i < testItems.size(); i++) {
                String itemBundleFilePath = testItems.get(i).bundleFilePath;
                if (itemBundleFilePath.equals(SharedPreferenceUtils.getChosenBusinessAssetsPath(mContext))) {
                    mSpinerChooseRNBusiness.setSelection(i);
                    break;
                }
            }
        }
    }

    class MyReactNativeBusinessAdapter extends BaseAdapter{

        List<ReactNativeBusinessInfo> data;

        public MyReactNativeBusinessAdapter(List<ReactNativeBusinessInfo> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(null == convertView){
                LayoutInflater layoutInflater=LayoutInflater.from(mContext);
                convertView= layoutInflater.inflate(R.layout.support_simple_spinner_dropdown_item, null);
            }
            ((TextView) convertView).setText(data.get(position).displayName);
            return convertView;
        }
    }


}
